﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Dgg.Anug.Cqrs.Core.Application.DefectHandling.Commands;
using Dgg.Anug.Cqrs.Core.Infrastructure.Validation;
using NUnit.Framework;

namespace Dgg.Anug.Cqrs.Core.Tests.Infrastructure.Validation
{
	[TestFixture]
	public class OpenIssueValidationTester
	{
		[Test]
		public void AllPropertiesValid_Valid()
		{
			ValidationService subject = new ValidationService();
			OpenIssue valid = new OpenIssue("title",
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				"description", "steps", "expected", "actual");
			Assert.That(() => subject.AssertValidity(valid), Throws.Nothing);
		}

		[Test, Combinatorial]
		public void AllPropertiesValid_WithoutOptionalFields_Valid(
			[Values(null, "")] string steps,
			[Values(null, "")] string expected,
			[Values(null, "")] string actual)
		{
			ValidationService subject = new ValidationService();
			OpenIssue valid = new OpenIssue("title",
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				"description", steps, expected, actual);
			Assert.That(() => subject.AssertValidity(valid), Throws.Nothing);
		}

		[Test]
		public void OptionalField_StepsTooLong_Invalid()
		{
			ValidationService subject = new ValidationService();
			OpenIssue valid = new OpenIssue("title",
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				new Guid("3a53bfb3-6149-4d91-b6ba-e51b4e556a5d"),
				"description", longString, null, null);
			Assert.That(() => subject.AssertValidity(valid), Throws.InstanceOf<ValidationException>());
		}

		private static readonly string zeroToNine = "0123456789";
		internal static string longString
		{
			get
			{
				var sb = new StringBuilder(3000);
				for (int i = 0; i < 300; i++)
				{
					sb.Append(zeroToNine);
				}
				return sb.ToString();
			}
		}
	}
}
